﻿#include "hzpch.h"
#include "OpenGLUniformBuffer.h"

#include <glad/glad.h>

namespace Hazal
{
	COpenGLUniformBuffer::COpenGLUniformBuffer(uint32_t unSize, uint32_t unBinding)
	{
		glCreateBuffers(1, &m_unRendererID);
		glBindBuffer(GL_UNIFORM_BUFFER, m_unRendererID);
		/// glBufferData(GL_UNIFORM_BUFFER, unSize, nullptr, GL_DYNAMIC_DRAW);
		glNamedBufferData(m_unRendererID, unSize, nullptr, GL_DYNAMIC_DRAW); /// 通过id来设置缓冲, 而非target

		/// 将缓冲区对象绑定到着色器的指定绑定点
		glBindBufferBase(GL_UNIFORM_BUFFER, unBinding, m_unRendererID);
	}

	COpenGLUniformBuffer::~COpenGLUniformBuffer()
	{
		glDeleteBuffers(1, &m_unRendererID);
	}

	void COpenGLUniformBuffer::Bind() const
	{
		glBindBuffer(GL_UNIFORM_BUFFER, m_unRendererID);
	}

	void COpenGLUniformBuffer::Unbind() const
	{
		glBindBuffer(GL_UNIFORM_BUFFER, 0);
	}

	void COpenGLUniformBuffer::SetData(const void* pData, uint32_t unSize, uint32_t unOffset)
	{
		Bind();
		/// glBufferSubData(GL_UNIFORM_BUFFER, unOffset, unSize, pData);
		glNamedBufferSubData(m_unRendererID, unOffset, unSize, pData);
		Unbind();
	}
}